
//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址，如： http://localhost:8083
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名，如：/uimcardprj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);


document.write("<script src=\"https://cdn.staticfile.org/axios/0.18.0/axios.min.js\"></script>");

var apiUrl = localhostPaht+projectName;

let api = {
    post: (url, args) => {
        return new Promise((resolve, reject) => {
            axios({
                url: apiUrl + url,
                method: 'post',
                data: args,
                transformRequest: [function (data) {
                    // Do whatever you want to transform the data
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(res => {
                if(res.data.message=='用户未登录'){
                    window.open(apiUrl+"/login.html")
                }else{
                    resolve(res.data);
                }

                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    get: (url) => {
        return new Promise((resolve, reject) => {
            axios({
                url: apiUrl + url,
                method: 'get'
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    postbyhost: (host, url, args) => {
        return new Promise((resolve, reject) => {
            axios({
                url: host + url,
                method: 'post',
                data: args
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    getbyhost: (host, url) => {
        return new Promise((resolve, reject) => {
            axios({
                url: host + url,
                method: 'post'
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    postJson: (url, args) => {
        return new Promise((resolve, reject) => {
            axios({
                url: apiUrl + url,
                method: 'post',
                data: args,
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },

}


